/**
  ******************************************************************************
  * @file    nuc it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
**/
#include "nuc970.h"
#include "sys.h"
#include "os_obj.h"
#include "dataproc.h"

void SwTestInit(void);


rt_thread_t         tid_SwTest1Task;
rt_thread_t         tid_SwTest2Task;
rt_thread_t         tid_SwTest3Task;

rt_sem_t            sid_SwTest1;
rt_sem_t            sid_SwTest2;
rt_sem_t            sid_SwTest3;

uint8_t testBuff[8];
uint32_t SwCnt = 0;

/*
*********************************************************************************************************
*                                          sw
*
* Description : Create application tasks.
*
* Argument(s) : none
*
* Return(s)   : none
*
* Note(s)     : none.
*********************************************************************************************************
*/
void SwTestTask(void *argument)
{
    SwTestInit();

    for(;;)
    {
        rt_sem_take(os_obj.sid_SwTest, 1000);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        rt_sem_release(sid_SwTest1);
    }
}

void SwTest1(void *argument)
{
    for(;;)
    {
        rt_sem_take(sid_SwTest1, 1000);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        rt_sem_release(sid_SwTest2);
    }
}

void SwTest2(void *argument)
{

    for(;;)
    {
        rt_sem_take(sid_SwTest2, 1000);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        rt_sem_release(sid_SwTest3);
    }
}

void SwTest3(void *argument)
{

    for(;;)
    {
        rt_sem_take(sid_SwTest3, 1000);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
        Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff); Uint16ToAscii(SwCnt++, 3, testBuff);
    }
}


void SwTestInit(void)
{
    tid_SwTest1Task = rt_thread_create("SwTest1",SwTest1,0,TASK_TASK_SW_TEST_STK_SIZE,TASK_TASK_SW_TEST_PRIO,10);
    tid_SwTest2Task = rt_thread_create("SwTest2",SwTest2,0,TASK_TASK_SW_TEST_STK_SIZE,TASK_TASK_SW_TEST_PRIO,10);
    tid_SwTest3Task = rt_thread_create("SwTest3",SwTest3,0,TASK_TASK_SW_TEST_STK_SIZE,TASK_TASK_SW_TEST_PRIO,10);
    
    sid_SwTest1 = rt_sem_create("sem sw1", 1, RT_IPC_FLAG_PRIO);
    sid_SwTest2 = rt_sem_create("sem sw2", 1, RT_IPC_FLAG_PRIO);
    sid_SwTest3 = rt_sem_create("sem sw3", 1, RT_IPC_FLAG_PRIO);
    
    rt_thread_startup(tid_SwTest1Task);
    rt_thread_startup(tid_SwTest2Task);
    rt_thread_startup(tid_SwTest3Task);
}
